﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：ucTipMessage自定义消息显示控件
//
//创建标识： 米海波 || 2011-06-15
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 消息展示控件
    /// </summary>
    public partial class ucTipMessage : UserControl
    {
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Browsable(true), Category("外观"), Description("设置边框颜色")]
        public Color BorderColor
        {
            get;
            set;
        }

        private string tipText;

        /// <summary>
        /// 显示文本
        /// </summary>
        [Browsable(true), Category("文本"), Description("设置显示的文本信息")]
        public string TipText
        {
            get
            {
                return tipText;
            }
            set
            {
                tipText = value;
                //自动计算大小
                if (this.AutoSize)
                    AutoToSize();
                this.Refresh();
            }
        }

        private Icon titleImage;

        /// <summary>
        /// 显示图标
        /// </summary>
        [Browsable(true), Category("外观"), Description("设置标题图片")]
        public Icon TitleIcon
        {
            get
            {
                return titleImage;
            }
            set
            {
                titleImage = value;
                this.width = value.Width + this.Margin.Left;
                this.Refresh();
            }
        }

        private bool _isShowIco = true;

        /// <summary>
        /// 是否显示图标
        /// </summary>
        [Browsable(true), Category("外观"), Description("是否限制图标")]
        public bool IsShowIco
        {
            get { return _isShowIco; }
            set { _isShowIco = value; this.Refresh(); }
        }

        /// <summary>
        /// 图标占用宽度
        /// </summary>
        private int width = 0;

        /// <summary>
        /// 初始对象身体内容
        /// </summary>
        public ucTipMessage()
        {
            InitializeComponent();
            this.Size = new Size(200, 22);
            this.AutoSize = true;
            this.TipText = "请输入提示消息";
            this.ForeColor = Color.FromArgb(61, 97, 129);
            this.BackColor = Color.FromArgb(255, 253, 244, 237);
            this.BorderColor = Color.FromArgb(255, 235, 200, 118);
        }

        /// <summary>
        /// 重写加载事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.IsShowIco)
            {
                //获取系统的错误提示图标，并克隆一份
                ErrorProvider error = new ErrorProvider();
                Icon ico = (Icon)error.Icon.Clone();
                error.Dispose();
                TitleIcon = ico;
            }

        }

        /// <summary>
        /// 重写绘制
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle rect = this.DisplayRectangle;
            Rectangle rect1;
            if (this.IsShowIco)
            {
                rect1 = new Rectangle(0, 0, this.width, this.Height - 1);
            }
            else
            {
                rect1 = new Rectangle(0, 0, 0, this.Height - 1);
            }
            Rectangle rect2 = new Rectangle(rect1.Width, 0, rect.Width - rect1.Width, this.Height);
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), rect);
            e.Graphics.DrawRectangle(new Pen(this.BorderColor), rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
            if (this.TitleIcon != null && this.IsShowIco)
                e.Graphics.DrawIcon(this.TitleIcon, this.Margin.Left, this.Margin.Top);
            DrawStringWrap(e.Graphics, this.Font, this.TipText, rect2);
            e.Graphics.Dispose();
        }

        /// <summary>
        /// 绘制文本自动换行（超出截断）
        /// </summary>
        /// <param name="graphic">绘图图面</param>
        /// <param name="font">字体</param>
        /// <param name="text">文本</param>
        /// <param name="recangle">绘制范围</param>
        private void DrawStringWrap(Graphics graphic, Font font, string text, Rectangle recangle)
        {
            List<string> textRows = GetStringRows(graphic, font, text, recangle.Width);
            int fontHeight = 16;// (int)(Math.Ceiling(graphic.MeasureString(text, font).Height));
            int maxRowCount = fontHeight == 0 ? 0 : recangle.Height / fontHeight;
            int drawRowCount = (maxRowCount < textRows.Count) ? maxRowCount : textRows.Count;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Far;
            for (int i = 0; i < drawRowCount; i++)
            {
                Rectangle fontRectanle = new Rectangle(recangle.Left + this.Margin.Left, this.Margin.Top + fontHeight * i, recangle.Width, fontHeight);
                graphic.DrawString(textRows[i], font, new SolidBrush(this.ForeColor), fontRectanle, sf);

            }

        }



        /// <summary>
        /// 将文本分行
        /// </summary>
        /// <param name="graphic">绘图图面</param>
        /// <param name="font">字体</param>
        /// <param name="text">文本</param>
        /// <param name="width">行宽</param>
        /// <returns></returns>
        private List<string> GetStringRows(Graphics graphic, Font font, string text, int width)
        {
            List<string> textRows = new List<string>();
            if (string.IsNullOrEmpty(text))
            {
                textRows.Add(string.Empty);
                return textRows;
            }
            text = text.Replace("\\r\\n", "\r\n");
            int RowBeginIndex = 0;
            int rowEndIndex = 0;
            int textLength = text.Length;
            for (int index = 0; index < textLength; index++)
            {
                rowEndIndex = index;
                if (index == textLength - 1)
                {
                    textRows.Add(text.Substring(RowBeginIndex));
                }
                else if (rowEndIndex + 1 < text.Length && text.Substring(rowEndIndex, 2) == "\r\n")
                {
                    textRows.Add(text.Substring(RowBeginIndex, rowEndIndex - RowBeginIndex));
                    rowEndIndex = index += 2;
                    RowBeginIndex = rowEndIndex;
                }
                else if (graphic.MeasureString(text.Substring(RowBeginIndex, rowEndIndex - RowBeginIndex + 1), font).Width > width)
                {
                    textRows.Add(text.Substring(RowBeginIndex, rowEndIndex - RowBeginIndex));
                    RowBeginIndex = rowEndIndex;
                }
            }
            return textRows;
        }

        /// <summary>
        /// 自动计算大小
        /// </summary>
        private void AutoToSize()
        {
            Graphics ghs = Graphics.FromHwnd(this.Handle);
            List<string> textRows;
            if (this.IsShowIco)
                textRows = GetStringRows(ghs, this.Font, this.tipText, this.Width - this.width);
            else
                textRows = GetStringRows(ghs, this.Font, this.tipText, this.Width);
            int fontHeight = 16;// (int)(Math.Ceiling(ghs.MeasureString(this.tipText, this.Font).Height));
            int maxRowCount = (fontHeight == 0) ? 0 : this.Height / fontHeight;
            //自动向下扩张
            if (textRows.Count > 1)
            {
                this.Height = fontHeight * textRows.Count + this.Margin.Top;
            }
            else
            {
                this.Height = 22;
            }
            ghs.Dispose();
            
        }

    }
}
